****************************************************************
*                                                              *
* Counterbalancing, Spatial Dependence, and Peer-Group Effects *
*                                                              *
* Political Science Research and Methods                       *
*                                                              *
* Tobias Boehmelt, Andrea Ruggeri, and Ulrich Pilster          *
*                                                              *
* This Version: April 15, 2015                                 *
*                                                              *
* Address Correspondance to: tbohmelt@essex.ac.uk              *
*                                                              *
****************************************************************

cd "/Users/tobiasbohmelt/Dropbox/Coups Spatial/PSRM Paper/2_Final Version/PSRM Replication/"

* Note: set working directory according to your location of the files.

************
* Figure 2 *
************

use "Counterbalancing Cross-Section 1970-2014.dta", clear

collapse effective_number, by(cow_reg)

label define Name 1 "North America" 2 "South America" 3 "Western Europe" 4 "Central & Eastern Europe" 5 "Sub-Saharan Africa" 6 "Middle East & North Africa" 7 "South Asia" 8 "Far East" 9 "Oceania", replace
label values cow_reg Name

graph hbar effective_number, over(cow_reg) bar(1, color(blue)) aspectratio(1)

**********************************
* Basic Information of Variables *
**********************************

set matsize 8000

use "Monadic Baseline.dta", clear

sum effective_number polity2 durable ny_gdp_pcap_kd sp_pop_totl fuel_i war_total army_centrality lag_DV 

**********************************************
* Moran's I Descriptives - Table 1, Last Row *
**********************************************

spatwmat using "inverse_coup", name(W)standardize
spatgsa lag_DV, w(W) m

spatwmat using "inverse_coup_lost", name(W)standardize
spatgsa lag_DV, w(W) m

spatwmat using "Joint_Demo II", name(W)standardize
spatgsa lag_DV, w(W) m

*****************************************************
* Model Estimations for Coup Any - Table 1, Model 1 *
*****************************************************

clear
pr drop _all
set matsize 8000
set more off
version 9                                                    

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 3497

clear
spatwmat using "inverse_coup", name(W)standardize
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)

drop _all

use "Monadic Baseline.dta", clear

global Y effective_number
global X polity2 durable ny_gdp_pcap_kd sp_pop_totl fuel_i war_total army_centrality lag_DV country_fe2-country_fe145 year_fe2- year_fe30
global Z lag_DV
mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)
matrix SL = W*Z
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) vce(oim)
ml init OLSb, skip
ml init rho:_cons=0
ml init sigma:_cons=`OLSsigma'
ml max, difficult ltolerance(1e-8) tolerance(1e-8)

*****************************************************************
* Model Estimations for Coup Counterpart Won - Table 1, Model 2 *
*****************************************************************

clear
pr drop _all
set matsize 8000
set more off
version 9                                                         

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 3497

clear
spatwmat using "inverse_coup_lost", name(W)standardize
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)

drop _all

use "Monadic Baseline.dta", clear

global Y effective_number
global X polity2 durable ny_gdp_pcap_kd sp_pop_totl fuel_i war_total army_centrality lag_DV country_fe2-country_fe145 year_fe2- year_fe30
global Z lag_DV
mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)
matrix SL = W*Z
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) vce(oim)
ml init OLSb, skip
ml init rho:_cons=0
ml init sigma:_cons=`OLSsigma'
ml max, difficult ltolerance(1e-8) tolerance(1e-8)

**********************************************************
* Model Estimations for Non-Democracy - Table 1, Model 3 *
**********************************************************

clear
pr drop _all
set matsize 8000
set more off
version 9

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 3497

clear
spatwmat using "Joint_Demo II.dta", name(W)standardize
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)

drop _all

use "Monadic Baseline.dta", clear

global Y effective_number
global X polity2 durable ny_gdp_pcap_kd sp_pop_totl fuel_i war_total army_centrality lag_DV country_fe2-country_fe145 year_fe2- year_fe30
global Z lag_DV
mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)
matrix SL = W*Z
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) vce(oim)
ml init OLSb, skip
* Initial estimate at 0.001558, but no SE.
ml init rho:_cons=0.00001
ml init sigma:_cons=`OLSsigma'
ml max, difficult ltolerance(1e-8) tolerance(1e-8)

***********************************************************
* Model Estimations for m-STAR Model I - Table 1, Model 4 *
***********************************************************

clear
pr drop _all
set more off
set matsize 8000
                                                     
program define splag_ll

args lnf mu rho1 rho2 sigma
tempvar A rSL1 rSL2 
gen double `rSL1'=`rho1'*SL1
gen double `rSL2'=`rho2'*SL2
scalar p1 = `rho1'
scalar p2 = `rho2'
matrix p1W1 = p1*W1
matrix p2W2 = p2*W2
matrix IpW = I_n - p1W1 - p2W2
qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
scalar A = `A'
qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'-`mu', 0, `sigma'))
end

spatwmat using "inverse_coup", name(W1)standardize

spatwmat using "Joint_Demo II.dta", name(W2)standardize

drop _all
use "Monadic Baseline.dta", clear
global nobs = 3497
matrix I_n = I($nobs)
global Y effective_number
global X polity2 durable ny_gdp_pcap_kd sp_pop_totl fuel_i war_total army_centrality lag_DV country_fe2-country_fe145 year_fe2- year_fe30
global Z lag_DV
mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)
matrix SL1 = W1*Z
svmat SL1, n(SL1)
matrix SL2 = W2*Z
svmat SL2, n(SL2)

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

ml model lf splag_ll (mu: $Y=$X) (rho1:) (rho2:) (sigma:)
ml init OLSb
ml init rho1:_cons=0
ml init rho2:_cons=0
ml init sigma:_cons=`OLSsigma'
ml max

***********************************************************
* Model Estimations for m-STAR Model I - Table 1, Model 5 *
***********************************************************

clear
pr drop _all
set more off
set matsize 8000                                                      

program define splag_ll

args lnf mu rho1 rho2 sigma
tempvar A rSL1 rSL2 
gen double `rSL1'=`rho1'*SL1
gen double `rSL2'=`rho2'*SL2
scalar p1 = `rho1'
scalar p2 = `rho2'
matrix p1W1 = p1*W1
matrix p2W2 = p2*W2
matrix IpW = I_n - p1W1 - p2W2
qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
scalar A = `A'
qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'-`mu', 0, `sigma'))
end

spatwmat using "inverse_coup_lost", name(W1)standardize

spatwmat using "Joint_Demo II.dta", name(W2)standardize

drop _all
use "Monadic Baseline.dta", clear
global nobs = 3497
matrix I_n = I($nobs)
global Y effective_number
global X polity2 durable ny_gdp_pcap_kd sp_pop_totl fuel_i war_total army_centrality lag_DV country_fe2-country_fe145 year_fe2- year_fe30
global Z lag_DV
mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)
matrix SL1 = W1*Z
svmat SL1, n(SL1)
matrix SL2 = W2*Z
svmat SL2, n(SL2)

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

ml model lf splag_ll (mu: $Y=$X) (rho1:) (rho2:) (sigma:)
ml init OLSb
ml init rho1:_cons=0
ml init rho2:_cons=0
ml init sigma:_cons=`OLSsigma'
ml max

***********
* Table 2 *
***********

use "Substantive Effects.dta", clear 

list estimate lb ub if sample<3

**************************************************************************************************
* Figure 3 (Note: graph notations modified after creating the figure; see gph-file in directory) *
**************************************************************************************************

use "Substantive Effects.dta", clear 

label define Variables 1 "Wy: Coups All" 2 "Wy: Coups All" 3 "Wy: Coups Outcome" 4 "Wy: Coups Outcome" 5 "Wy: Non-Democracy" 6 "Wy: Non-Democracy" 7 "Wy: Coups All" 8 "Wy: Coups All" 9 "Wy: Non-Democracy" 10 "Wy: Non-Democracy" 11 "Wy: Coups Outcome" 12 "Wy: Coups Outcome" 13 "Wy: Non-Democracy" 14 "Wy: Non-Democracy", replace

label values variables Variables

twoway (scatter variables estimate if sample==3, sort) (scatter variables estimate if sample==4, sort) (rcap lb ub variables if sample==3, horizontal) (rcap lb ub variables if sample==4, horizontal), xline(0, lpattern(dash)) legend(on) scheme(lean1) name(graph2, replace)
